// 路由
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('@/views/Layout.vue'),
      children: [
        {
          path: '/home',
          components: {
            default: () => import('@/views/Home.vue'),
            sidebar: () => import('@/views/Sidebar.vue')
          },
          meta: { title: '首页' }
        },
        {
          path: '/about',
          components: {
            default: () => import('@/views/About.vue'),
            sidebar: () => import('@/views/Sidebar.vue')
          },
          meta: { title: '关于' },
          children: [
            {
              path: 'team',
              component: () => import('@/views/Team.vue')
            },
            {
              path: 'history',
              component: () => import('@/views/History.vue')
            }
          ]
        },
      ]
    },
    {
      path: '/user/:id',
      component: () => import('@/views/User.vue')
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    return { top: 0 };
  }
})

router.beforeEach((to, from, next) => {
  if (to.path === '/about') {
    if (confirm('确定要访问关于页面吗？')) {
      next()
    } else {
      next(false)
    }
  } else {
    next()
  }

  document.title = to.meta.title

})

export default router
